package com.spukmk3me.impl.libgdx;

import com.spukmk3me.Framework;
import com.spukmk3me.util.Util;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.spukmk3me.io.FileSystem;
import com.spukmk3me.io.userinput.ButtonInput;
import com.spukmk3me.io.userinput.InputSystem;
import com.spukmk3me.io.userinput.PointerInput;
import com.spukmk3me.resource.ResourceManager;
import com.spukmk3me.scene.Scene;
import com.spukmk3me.scenepack.pack0.DefaultScene;
import com.spukmk3me.sound.SoundPlayer;
import com.spukmk3me.video.VideoAPI;

public class LibgdxFramework extends Framework
{
    public LibgdxFramework( SpriteBatch batch )
    {
        init( batch );
        
        /*m_running = true;
        m_maxDeltaMSPF = 33; // Default value for ~30 FPS*/

        Framework.PLATFORM = this;
    }
    
    private void init( SpriteBatch batch )
    {
        Util.initialiseRandomSeed( System.currentTimeMillis() );
        m_fSys = new LibgdxFileSystem();
        m_vAPI = new LibgdxVideoAPI( batch );
        m_sndP = new LibgdxSoundPlayer();
        m_vAPI.init();
        m_rMan = new ResourceManager();
        m_rMan.addResourceLoader( new LibgdxResourceCreator() );
        m_inputSys = new LibgdxInputSystem();
        m_pInput = m_inputSys.getPointerInput();
        m_bInput = new LibgdxButtonInput();
        
        
        InputMultiplexer iMultiplexer = new InputMultiplexer( m_inputSys );
        
        Gdx.input.setInputProcessor( iMultiplexer );
        Gdx.input.setCatchBackKey( true );
    }
    
    @Override
    public final FileSystem fsys()
    {
        return m_fSys;
    }

    @Override
    public final ResourceManager rman()
    {
        return m_rMan;
    }

    @Override
    public final Scene createScene()
    {
        return new DefaultScene( m_vAPI );
    }

    @Override
    public final VideoAPI vapi()
    {
        return m_vAPI;
    }
    
    @Override
    public final SoundPlayer sndp()
    {
        return m_sndP;
    }
    
    @Override
    public PointerInput pnti()
    {
        return m_pInput;
    }
    
    @Override
    public ButtonInput btni()
    {
        return m_bInput;
    }
    
    @Override
    public InputSystem isys()
    {
        return m_inputSys;
    }

    /*public final void setGameProcessor( LibgdxGameProcessor processor )
    {
        m_gameProcessor = processor;
    }
    
    @Override
    public final void render()
    {
        if ( m_running )
        {
            long deltaTimeMS = (long)(Gdx.graphics.getDeltaTime() * 1000);
            deltaTimeMS = Math.min( deltaTimeMS, m_maxDeltaMSPF );
            
            int deltaTimeI = Util.convertMilisecToFP( (int)deltaTimeMS );
            
            m_pInput.process();
            m_bInput.process( deltaTimeI );
            
            if ( m_gameProcessor != null )
                m_running = !m_gameProcessor.process( (int)deltaTimeMS );
            
            if ( !m_running )
                Gdx.app.exit();
        }
    }

    @Override
    public synchronized final void pause()
    {
        m_gameProcessor.pause();
    }

    @Override
    public synchronized final void resume()
    {
        m_vAPI.markGLCreateContext();
        m_gameProcessor.resume();
    }

    @Override
    public final void dispose()
    {
        m_gameProcessor.quit();
        m_scene.dropChildren( m_scene.getRoot() );
        m_sndP.clearSounds();
        m_rMan.clearAllUnusedResource();
        m_vAPI.cleanup();
    }
    
    public final void setMaximumDeltaMSPerFrame( long deltaMS )
    {
        m_maxDeltaMSPF = deltaMS;
    }
    
    public long getMaximumDeltaMSPerFrame()
    {
        return m_maxDeltaMSPF;
    }*/
    
    private LibgdxVideoAPI      m_vAPI;
    private LibgdxSoundPlayer   m_sndP;
    private LibgdxFileSystem    m_fSys;
    private ResourceManager     m_rMan;
    private PointerInput        m_pInput;
    private LibgdxButtonInput   m_bInput;
    private LibgdxInputSystem   m_inputSys;
    /*private LibgdxGameProcessor m_gameProcessor;
    
    private long                m_maxDeltaMSPF;
    private boolean             m_running;*/
}
